<template>
  <el-card style="margin: 0 15px">
    <el-row :gutter="10">
      <el-form ref="contractCreateForm" :model="contractCreateFormData" :rules="rules" size="mini"
        label-width="100px" label-position="top">
        <el-col :span="12">
          <el-form-item label="合同名称" prop="contractName">
            <el-input v-model="contractCreateFormData.contractName" placeholder="请输入合同名称" :maxlength="20"
              show-word-limit clearable prefix-icon='el-icon-document-copy' :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户" prop="clientId">
            <el-select v-model="contractCreateFormData.clientId" placeholder="请选择客户" filterable
              :style="{width: '100%'}">
              <el-option v-for="(item, index) in clientIdOptions" :key="index" :label="item.clientName"
                :value="item.clientId" :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="销售员" prop="salesmanId">
            <el-select v-model="contractCreateFormData.salesmanId" placeholder="请选择销售员" filterable
              :style="{width: '100%'}">
              <el-option v-for="(item, index) in salesmanIdOptions" :key="index" :label="item.salesmanName"
                :value="item.salesmanId" :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="履行状态" prop="fulfilled">
            <el-select v-model="contractCreateFormData.fulfilled" placeholder="请选择履行状态" clearable
              :style="{width: '100%'}">
              <el-option v-for="(item, index) in fulfilledOptions" :key="index" :label="item.label"
                :value="item.value" :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="合同描述" prop="contractDescription">
            <el-input v-model="contractCreateFormData.contractDescription" type="textarea"
              placeholder="请输入合同描述" :maxlength="100" show-word-limit :autosize="{minRows: 4, maxRows: 4}"
              :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否已付清款项" prop="paid">
            <el-select v-model="contractCreateFormData.paid" placeholder="请输入是否已付清款项"
              :style="{width: '100%'}">
              <el-option v-for="(item, index) in paidOptions" :key="index" :label="item.label"
                :value="item.value" :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="总金额" prop="contractTotalCost">
            <el-input v-model="contractCreateFormData.contractTotalCost" placeholder="总金额根据所选商品生成"
              :disabled='true' clearable prefix-icon='el-icon-star-on' :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="已付款金额" prop="hasPaid">
            <el-input v-model="contractCreateFormData.hasPaid" placeholder="请输入已付款金额" clearable
              prefix-icon='el-icon-star-off' :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item size="large">
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
  </el-card>
</template>
<script>
import { clientAll,salesmanAll,contractCreate } from '@/api/sale'

export default {
  components: {},
  props: [],
  data() {
    return {
      contractCreateFormData: {
        contractName: undefined,
        clientId: '',
        salesmanId: '',
        fulfilled: 0,
        contractDescription: undefined,
        paid: 0,
        contractTotalCost: 0,
        hasPaid: 0,
      },
      rules: {
        contractName: [{
          required: true,
          message: '请输入合同名称',
          trigger: 'blur'
        }],
        clientId: [{
          required: true,
          message: '请选择客户',
          trigger: 'change'
        }],
        salesmanId: [{
          required: true,
          message: '请选择销售员',
          trigger: 'change'
        }],
        fulfilled: [{
          required: true,
          message: '请选择履行状态',
          trigger: 'change'
        }],
        contractDescription: [{
          required: true,
          message: '请输入合同描述',
          trigger: 'blur'
        }],
        paid: [{
          required: true,
          message: '请输入是否已付清款项',
          trigger: 'change'
        }],
        contractTotalCost: [{
          required: true,
          message: '总金额根据所选商品生成',
          trigger: 'blur'
        }],
        hasPaid: [{
          required: true,
          message: '请输入已付款金额',
          trigger: 'blur'
        }],
      },
      clientIdOptions: [],
      salesmanIdOptions: [],
      fulfilledOptions: [{
        "label": "未履行",
        "value": 0
      }, {
        "label": "正在履行",
        "value": 1
      }],
      paidOptions: [{
        "label": "未付清",
        "value": 0
      }, {
        "label": "已付清",
        "value": 1
      }],
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getClientAll()
    this.getSalesmanAll()
  },
  mounted() {},
  methods: {
    // 合同创建表单提交
    submitForm() {
      this.$refs['contractCreateForm'].validate(valid => {
        if (!valid) return
        contractCreate(this.contractCreateFormData).then()
      })
    },
    // 表单置空
    resetForm() {
      this.$refs['contractCreateForm'].resetFields()
    },
    // 获取所有客户
    getClientAll(){
      clientAll().then(res => {
        this.clientIdOptions = res.data.clientList
      })
    },
    // 获取所有销售员
    getSalesmanAll(){
      salesmanAll().then(res => {
        console.log(res);
        this.salesmanIdOptions = res.data.salesmanList
      })
    },
  
  }
}

</script>
<style scoped>

</style>
